package com.aso114.project.mvp.activity;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aso114.project.R;
import com.aso114.project.bean.BannerBean;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;
import com.aso114.project.util.MySharedPreferences;
import com.aso114.project.util.SystemBarHelper;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @{# SplashActivity.java Create on 2013-5-2 下午9:10:01
 * <p>
 * class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法
 * (2)是，则进入GuideActivity；否，则进入MainActivity (3)3s后执行(2)操作
 * <p>
 * <p>
 * Copyright: Copyright(c) 2013
 * </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a> 启动的时候顺便获取定位
 */
public class StarActivity extends Activity {
    @BindView(R.id.starlogo)
    ImageView starlogo;

    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.layout_agreement)
    LinearLayout layoutAgreement;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.cb_agreement)
    ImageView cbAgreement;
    @BindView(R.id.ch_delete)
    CheckBox chDelete;
    @BindView(R.id.explaintlayout)
    RelativeLayout explaint;
    @BindView(R.id.iv_splash_holder)
    ImageView ivSplashHolder;
    @BindView(R.id.iv_skip_view)
    TextView ivSkipView;
    @BindView(R.id.project_name)
    TextView projectName;
    @BindView(R.id.down_ly)
    LinearLayout downLy;
    @BindView(R.id.splash_ly)
    LinearLayout splashLy;
    int agree = 0;//0 没有勾选  1勾选
    private static final int GO_HOME = 1000;
    private static final long SPLASH_DELAY_MILLIS = 3000;
    Intent intent;
    private static final String SKIP_TEXT = "跳过 %d";
    CountDownTimer timer;
    /**
     * Handler:跳转到不同界面
     */
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:

                    goHome();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staractivity);
        ButterKnife.bind(this);
        Glide.with(this).load(R.mipmap.logogif).into(starlogo);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        LoginSP.getInstance().setBanner(false);
        if (!LoginSP.getInstance().getFirstTost().equals("")) {
            explaint.setVisibility(View.GONE);
            if (LoginSP.getInstance().isSkip()) {//是否点击过跳过
                mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
            } else {
                CommentModel.getInstant().getAptiudeTest_banner(Constant.CateId, "3", new CallBack() {
                    @Override
                    public void onSuccess(Object o, boolean isSuccess, String msg) {
                        Helper.gotoLogin(isSuccess);//token失效去重新登陆
                        List<BannerBean> bannerList = (List<BannerBean>) o;
                        if (bannerList == null || bannerList.isEmpty()) {
                            goHome();
                        }
                        splashLy.setVisibility(View.VISIBLE);
                        setName();
                        Glide.with(StarActivity.this).load(bannerList.get(0).getLitpic()).into(ivSplashHolder);
                        ivSkipView.setVisibility(View.VISIBLE);
                        timer = new CountDownTimer(5000, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                ivSkipView.setText(String.format(SKIP_TEXT, millisUntilFinished / 1000));
                            }

                            @Override
                            public void onFinish() {
                                goHome();
                            }
                        }.start();
                    }

                    @Override
                    public void onFailure(String fail) {
                        goHome();
                    }
                });

            }

        }

        chDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {

                    System.out.println("选中");
                    agree = 1;
                } else {
                    System.out.println("取消选中");
                    agree = 0;
                }
            }
        });

    }

    private void setName() {
        String packageName = this.getPackageName();
        if ("com.servant.examination".equals(packageName)) {
            projectName.setText("中软公务员考试");
        } else if ("com.zhonglian.Public".equals(packageName)) {
            projectName.setText("中联公考通");
        } else if ("com.wanxue.valuable".equals(packageName)) {
            projectName.setText("万学公考宝典");
        } else if ("com.lianda.servants".equals(packageName)) {
            projectName.setText("联大公务员题库");
        } else if ("com.valuable.examination".equals(packageName)) {
            projectName.setText("中博公务员考试");
        }
    }

    private void goHome() {
        if (LoginSP.getInstance().getFirst().equals("")) {
            //表示第一次登陆 必须跳转去登陆
            intent = new Intent(StarActivity.this, LoginActivity.class);
            startActivity(intent);
        } else {
            if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                LoginSP.getInstance().setFirst("");
                intent = new Intent(StarActivity.this, LoginActivity.class);
                startActivity(intent);
            } else {
                intent = new Intent(StarActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }

        finish();
    }

    @OnClick({R.id.tv_agreement, R.id.tv_commit, R.id.iv_skip_view, R.id.iv_splash_holder})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_agreement:
                intent = new Intent(StarActivity.this, MessageExplaintActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_commit:
                if (agree == 0) {
                    Helper.showToast("您还未同意个人信息保护条款");
                } else {
                    LoginSP.getInstance().setFirstTost("1");
                    if (LoginSP.getInstance().getFirst().equals("")) {
                        //表示第一次登陆 必须跳转去登陆
                        intent = new Intent(StarActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else {
                        intent = new Intent(StarActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    finish();
                }
                break;
            case R.id.iv_skip_view:
                //跳过
                LoginSP.getInstance().setSkip(true);
                timer.cancel();
                goHome();
                break;
            case R.id.iv_splash_holder:
                //图片广告
                LoginSP.getInstance().setBanner(true);
                timer.cancel();
                goHome();
                break;
        }
    }

}